﻿using System;
using System.Runtime.Serialization;

namespace PMS.Exceptions
{
    /// <summary>
    /// Summary description for BusinessException.
    /// </summary>
    [Serializable]
    public class BusinessException : ApplicationException
    {
        public static BusinessException GetException(string message, Exception ex)
        {
            Logger.Error(ex);

            //DuyHB1
            if (ex is DataAccessConflictUpdateException)
            {
                return new BussinessConflictUpdateException(ex.Message, message, ex);
            }
            else if (ex is BussinessConflictUpdateException)
            {
                return ex as BussinessConflictUpdateException;
            }
            else if (ex is DataAccessException)
            {
                var dataEx = ex as DataAccessException;

                if (dataEx.ErrorCode > 0)
                {
                    var errorId = string.Empty;
                    if (dataEx.ErrorCode == 4060)
                    {
                        errorId = "InvalidDatabase";
                    }
                    else if (dataEx.ErrorCode == 547)
                    {
                        errorId = "ForeignKeyViolation";
                    }
                    else if (dataEx.ErrorCode == 18456)
                    {
                        errorId = "LoginFailed";
                    }


                    //errorId = App.Resource.MessageResources.GetString(dataEx.ErrorCode.ToString());
                    return new BusinessException(errorId, message, ex);
                }
                //else return new BusinessException(PMS.Resources.MessageResources.GetString(ex.Message), ex.InnerException);
            }

            return new BusinessException(ex.Message, ex);
        }

        private string errorId;
        public string ErrorId
        {
            get { return errorId; }
            private set { errorId = value; }
        }

        public override string Message
        {
            get
            {
                if (!string.IsNullOrEmpty(base.Message))
                    return base.Message;
                else if (base.InnerException != null)
                    return base.InnerException.Message;

                return base.Message;
            }
        }
        //protected BusinessException(string errorId, SerializationInfo info, StreamingContext context)
        //    : base(info, context)
        //{
        //    this.ErrorId = errorId;
        //}

        protected BusinessException(string errorId, string message, Exception innerException)
            : base(message, innerException)
        {
            this.ErrorId = errorId;
        }

        protected BusinessException(string message, Exception innerException)
            : this(string.Empty, message, innerException)
        {
        }

        protected BusinessException(string errorId)
            : this(errorId, string.Empty, null)
        {
        }
    }
}
